import { TaskBase } from '../TaskBase';
import { ObjectWithStore } from '../Tasks';

export class TaskWithdrawAll extends TaskBase {
    shouldRun(creep: Creep) {
        // 指定 rType 可以选择要跳过的资源类型
        return this.peekFirstResource(<AnyStoreStructure>this.target) != undefined;
    }

    _run(creep: Creep) {
        const thisResource = this.peekFirstResource(<AnyStoreStructure>this.target);
        return this.defaultProcess(creep.withdraw(<AnyStoreStructure>this.target, thisResource));
    }

    peekFirstResource(structure: AnyStoreStructure) {
        if (!('store' in structure)) {
            return undefined;
        }
        for (const rType in structure.store) {
            if (this.taskData.rType && this.taskData.rType === rType) {
                continue;
            }
            const amount = structure.store[this.taskData.rType] ?? 0;
            if (amount > 0) {
                return rType as ResourceConstant;
            }
        }
        return undefined;
    }
}